using System;
using System.Runtime.InteropServices;
using Microsoft.SharePoint;
using System.Linq;

namespace RGB.SharePoint.SPBlockFileTypes.Features.SPBlockFileTypes
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("c44c2b54-88ea-445e-8114-2a39a648b855")]
    public class SPBlockFileTypesEventReceiver : SPFeatureReceiver
    {
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPWeb web = properties.Feature.Parent as SPWeb;
            foreach (SPList list in web.Lists.OfType<SPDocumentLibrary>())
            {
                var eventReceiver = list.EventReceivers.OfType<SPEventReceiverDefinition>().SingleOrDefault(ev => ev.Class.Equals(typeof(BlockFileTypesEventReceiver).FullName));
                if (eventReceiver != null)
                {
                    eventReceiver.Delete();
                    list.RootFolder.Properties["ExtensionsList"] = string.Empty;
                    list.RootFolder.Properties["BlockType"] = "Allow";
                    list.RootFolder.Update();
                }
            }
        }
    }
}
